﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：公式设置
 */
var selectedRow;
var acceptClick;

var bootstrap = function ($, learun) {
    "use strict";
    var selectedRow = config.getParent(learun).selectedRow;
    var companyId = request("companyId");
    var ztId = request("ztId");
    var keyValue = request("keyValue");
    var item = {};

    var page = {
        init: function () {
            page.inittree();
            page.bind();
        },
        bind: function () {
            $("button").click(function () {
                var text = $("#formula").val();
                var id = $(this).attr("id");
                switch (id) {
                    case "comfirm":   //确认
                        alert("comfirm");
                        break;
                    case "cancel":    //取消
                        alert("cancel");
                        break;
                    case "preMonthTax":    //上月所得税
                    case "curMonthTax":    //本月所得税
                    case "curMonthDay":    //当月天数
                    case "preMonthMoney":    //上月金额
                        $("#formula").val(text + "$" + $(this).val() + "");
                        break;
                    case "mulAdd":     //连加
                        var items = $("#itemTree").lrtreeSet('getCheckNodesEx');
                        if (items.length > 0) {
                            var addStr = "";
                            for (var i = 0; i < items.length; i++) {
                                if ($.trim(text) != "" || i > 0) {
                                    addStr += "+$" + items[i].text + "";
                                } else {
                                    addStr += "$" + items[i].text + "";
                                }
                            }
                            $("#formula").val(text + addStr);
                        }
                        break;
                    case "mulReduce":  //连减少
                        var items = $("#itemTree").lrtreeSet('getCheckNodesEx');
                        if (items.length > 0) {
                            var addStr = "";
                            for (var i = 0; i < items.length; i++) {
                                if ($.trim(text) != "" || i > 0) {
                                    addStr += "-$" + items[i].text;
                                } else {
                                    addStr += "$" + items[i].text + "";
                                }
                            }
                            $("#formula").val(text + addStr);
                        }
                        break;

                    default:
                        $("#formula").val(text + $(this).val());
                        break;

                }
            })

        },
        inittree: function () {
            if (companyId != "" && ztId != '') {
                $('#itemTree').lrtree({
                    url: config.base_server() + 'gzitems/dropdownlist?companyId=' + companyId + '&ztId=' + ztId + '&access_token=' + config.getToken().access_token,
                    param: {},
                    nodeDbClick: page.treeNodeDbClick
                });

                $.ajax({
                    url: config.base_server() + 'gzitems/findById?id=' + keyValue + '&access_token=' + config.getToken().access_token,
                    type: "get",
                    dataType: "json",
                    success: function (result) {
                        item = result.data;
                        $("#formula").val(result.data.formula);
                    }
                });
            }
            if (!!selectedRow) {
                $('#sp_item').html(selectedRow.name);
            }
        },
        treeNodeDbClick: function (item) {
            var text = $("#formula").val();
            $("#formula").val(text + "$" + item.text);
        }
    };

    // 保存数据
    acceptClick = function (callBack) {
        var formula = $('#formula').val();
        var postData = item;
        postData.formula = formula;

        $.lrSaveForm(config.base_server() + 'gzitems/setFormula?keyValue=' + keyValue + '&access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
            // 保存成功后才回调
            if (!!callBack) {
                callBack();
            }
        });
    };

    page.init();
}


